{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Matrix([[2*x - 2*z**2, 2*y, 2*z*(-2*x + 2*z**2 + 1)]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import *\n",
    "import numpy as np\n",
    "\n",
    "x = Symbol('x')\n",
    "y = Symbol('y')\n",
    "z = Symbol('z')\n",
    "\n",
    "f = (x-z**2)**2 +y**2 + z**2 -1\n",
    "\n",
    "u = x*y\n",
    "\n",
    "def Com_SUrface(f):\n",
    "    fx = diff(f,x)\n",
    "    fy = diff(f,y)\n",
    "    fz = diff(f,z)\n",
    "    N = zeros(1, 3)\n",
    "    N[0] = fx\n",
    "    N[1] = fy\n",
    "    N[2] = fz\n",
    "    return N\n",
    "    \n",
    "\n",
    "def com_Du(u):\n",
    "    ux = diff(u,x)\n",
    "    uy = diff(u,y)\n",
    "    uz = diff(u,z)\n",
    "    gradu = zeros(1,3)\n",
    "    gradu[0] = ux\n",
    "    gradu[1] = uy\n",
    "    gradu[2] = uz    \n",
    "    return gradu\n",
    "\n",
    "def unitnormal(N):\n",
    "    uNval = sqrt(N[0]**2 + N[1]**2 + N[2]**2)\n",
    "    uNx = N[0]/uNval\n",
    "    uNy = N[1]/uNval\n",
    "    uNz = N[2]/uNval\n",
    "    uN = zeros(1,3)\n",
    "    uN[0] = uNx\n",
    "    uN[1] = uNy\n",
    "    uN[2] = uNz    \n",
    "    return uN\n",
    "\n",
    "def Grandus(Du, uN):\n",
    "    \n",
    "    b1 = Du[0]*uN[0]+Du[1]*uN[1]+Du[2]*uN[2]\n",
    "    b = b1*uN\n",
    "    Dsu = Du - b\n",
    "    return Dsu\n",
    "\n",
    "def com_sourceF(uN,Dsu):\n",
    "    diverDsu = diff(Dsu[0],x)+diff(Dsu[1],y) + diff(Dsu[2],z)\n",
    "    d = 0\n",
    "    for i in range(3):\n",
    "        gradDsu = zeros(1,3)\n",
    "        gradDsu[0] = diff(Dsu[i], x)\n",
    "        gradDsu[1] = diff(Dsu[i], y)\n",
    "        gradDsu[2] = diff(Dsu[i], z)\n",
    "        gradient = (gradDsu[0]*uN[0] + gradDsu[1]*uN[1] + gradDsu[2]*uN[2])*uN[i]\n",
    "        d = d + gradient\n",
    "    F = - diverDsu + d\n",
    "    return F\n",
    "\n",
    "N = Com_SUrface(f)\n",
    "N \n",
    "simplify(N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Matrix([[(x - z**2)/sqrt(y**2 + z**2*(2*x - 2*z**2 - 1)**2 + (x - z**2)**2), y/sqrt(y**2 + z**2*(2*x - 2*z**2 - 1)**2 + (x - z**2)**2), z*(-2*x + 2*z**2 + 1)/sqrt(y**2 + z**2*(2*x - 2*z**2 - 1)**2 + (x - z**2)**2)]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Du = com_Du(u)\n",
    "Du\n",
    "\n",
    "uN = unitnormal(N)\n",
    "uN\n",
    "simplify(uN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Matrix([[y - (2*x - 2*z**2)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2), x - 2*y*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2), -(-4*z*(x - z**2) + 2*z)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dsu = Grandus(Du,uN)\n",
    "Dsu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Matrix([[y*(y**2 + z**2*(2*x - 2*z**2 - 1)**2 + (x - z**2)**2 - (x - z**2)*(2*x - z**2))/(y**2 + z**2*(2*x - 2*z**2 - 1)**2 + (x - z**2)**2), (x*(y**2 + z**2*(2*x - 2*z**2 - 1)**2 + (x - z**2)**2) - y**2*(2*x - z**2))/(y**2 + z**2*(2*x - 2*z**2 - 1)**2 + (x - z**2)**2), y*z*(2*x - z**2)*(2*x - 2*z**2 - 1)/(y**2 + z**2*(2*x - 2*z**2 - 1)**2 + (x - z**2)**2)]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = simplify(Dsu)\n",
    "M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-8*y**2*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 2*y*(2*y*(8*y**2*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - 2*y*(-8*x*y**2/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 2*x/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - 4*y**2*(2*x - 2*z**2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + (2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - 2*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (2*x - 2*z**2)*(-2*y*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - 2*y*(2*x*y*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + y*(2*x - 2*z**2)*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 4*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + 1)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (-4*z*(x - z**2) + 2*z)*(-2*y*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - 2*y*(2*x*y*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - 4*y*z/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + 2*y*(-8*x*y**2/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 2*x/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - 4*y**2*(2*x - 2*z**2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + (2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (2*x - 2*z**2)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + (2*x - 2*z**2)*(2*y*(4*y*(2*x - 2*z**2)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - (2*x - 2*z**2)*(-8*x*y**2/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 2*x/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - 4*y**2*(2*x - 2*z**2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + (2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + 1)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (2*x - 2*z**2)*(-(2*x - 2*z**2)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - (2*x - 2*z**2)*(2*x*y*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + y*(2*x - 2*z**2)*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 4*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - 2*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (-4*z*(x - z**2) + 2*z)*(4*z*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - (2*x - 2*z**2)*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - (2*x - 2*z**2)*(2*x*y*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - 4*y*z/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (2*x - 2*z**2)*(2*x*y*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + y*(2*x - 2*z**2)*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 4*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (-4*z*(x - z**2) + 2*z)*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + (-4*z*(x - z**2) + 2*z)*(2*y*(4*y*(-4*z*(x - z**2) + 2*z)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - (-4*z*(x - z**2) + 2*z)*(-8*x*y**2/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 2*x/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - 4*y**2*(2*x - 2*z**2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + (2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (2*x - 2*z**2)*(4*z*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - (-4*z*(x - z**2) + 2*z)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - (-4*z*(x - z**2) + 2*z)*(2*x*y*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + y*(2*x - 2*z**2)*(-4*x + 4*z**2 + 4*z*(-4*z*(x - z**2) + 2*z))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) + 4*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (-4*z*(x - z**2) + 2*z)*(-(-4*z*(x - z**2) + 2*z)*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - (-4*z*(x - z**2) + 2*z)*(2*x*y*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - 4*y*z/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) - (2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))*(-4*x + 12*z**2 + 2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (-4*z*(x - z**2) + 2*z)*(2*x*y*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2) - 4*y*z/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)*(4*z*(2*x - 2*z**2) - (-4*z*(x - z**2) + 2*z)*(-8*x + 24*z**2 + 4)/2)/(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)**(3/2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + (2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))*(-4*x + 12*z**2 + 2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + 4*(2*x*y/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2) + y*(2*x - 2*z**2)/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2))/sqrt(4*y**2 + (2*x - 2*z**2)**2 + (-4*z*(x - z**2) + 2*z)**2)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F = com_sourceF(uN,Dsu)\n",
    "F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "G = simplify(F)\n",
    "G"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
